<?php

namespace Biz\model\brandAccount;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\model\Phone as PhoneModel;
use hhz\constant\basic\UserExtendPhoneScene;
use Doraemon\tools\Log;
use Doraemon\service\Member\PhoneCrypto;

class BrandAccountAuth extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_account_auth';

    const AUTH_STATUS_DEFAULT = 0;//待审核
    const AUTH_STATUS_PASS = 1;//审核通过
    const AUTH_STATUS_NOPASS = 2;//审核未通过
    const AUTH_STATUS_USED = 3;//使用中
    const AUTH_STATUS_EXPIRE = 4;//认证过期
    const AUTH_STATUS_DEL = 9;//失效
    const AUTH_STATUS_MAP = [
        self::AUTH_STATUS_DEFAULT => '待审核',
        self::AUTH_STATUS_PASS => '审核通过',
        self::AUTH_STATUS_NOPASS => '审核未通过',
        self::AUTH_STATUS_USED => '使用中',
        self::AUTH_STATUS_EXPIRE => '认证过期',
    ];

    //消息通知code
    const SEND_CODE_PASS = 501;//通过
    const SEND_CODE_NOPASS = 502;//拒绝
    const SEND_CODE_OVER = 503;// 过期


    public static function getAllByWhere($where = [], $files = '*')
    {
        $data = self::select($files, $where);
        return $data;
    }

    public static function getOneByWhere($where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function insertData($data)
    {
        if (!isset($data['add_time'])) {
            $data['add_time'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = date('Y-m-d H:i:s');
        }
        if ($data['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['phone'], UserExtendPhoneScene::BRAND_AUTH_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['phone_encrypt_id'] = $encryptPhoneId;
            $data['phone'] = PhoneCrypto::mask($data['phone']);
        } else {
            $data['phone_encrypt_id'] = 0;
            $data['phone'] = '';
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = date('Y-m-d H:i:s');
        }
        if ($data['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['phone'], UserExtendPhoneScene::BRAND_AUTH_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['phone_encrypt_id'] = $encryptPhoneId;
            $data['phone'] = PhoneCrypto::mask($data['phone']);
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}